package local.app.servers;


import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import local.app.authentiction.AuthenticationRSA;

public abstract class Server {
    private String nameServer;
    private String ipAddress;
    private final int port = 22;
    private boolean isPingSuccess;
    private boolean isAuthenticationSuccess;
    private String osVersion;

    public Session session;


    public Server(String nameServer, String ipAddress) {
        this.nameServer = nameServer;
        this.ipAddress = ipAddress;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    abstract public Session setConfigAndReturnSession() throws JSchException;

    public Session setConfigAndReturnSession(String userName, String pathToPrivateKey, String passphrase) throws JSchException {
        JSch jSch = new JSch();
        jSch.addIdentity(pathToPrivateKey, passphrase);
        Session session = jSch.getSession(userName, getIpAddress(), getPort());
        session.setConfig("StrictHostKeyChecking", "no");
        return session;
    }

    public Session setConfigAndReturnSession(String userName, String password) throws JSchException {
        Session session = new JSch().getSession(userName, getIpAddress(), getPort());
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        return session;
    }

    void createSession (String userName, String pathToPrivateKey, String password) throws JSchException {
        this.session = this.setConfigAndReturnSession(userName,
                pathToPrivateKey, password);
        if (session != null) {
            System.out.println("Server " + this.ipAddress + " session is created");
        }
    }


    public int getPort() {
        return port;
    }


    public String getNameServer() {
        return nameServer;
    }

    public boolean isPingSuccess() {
        return isPingSuccess;
    }


    public void setPingSuccess(boolean pingSuccess) {
        isPingSuccess = pingSuccess;
    }

    public boolean isAuthenticationSuccess() {
        return isAuthenticationSuccess;
    }

    public void setAuthenticationSuccess(boolean authenticationSuccess) {
        isAuthenticationSuccess = authenticationSuccess;
    }

    public String getOsVersion() {
        return osVersion;
    }

    public void setOsVersion(String osVersion) {
        this.osVersion = osVersion;
    }

    @Override
    public String toString() {
        return "Server{" +
                "name='" + nameServer + '\'' +
                ", ipAddress='" + ipAddress + '\'' +
                ", port=" + port +
                ", isPingSuccess=" + isPingSuccess +
                ", isAuthenticationSuccess=" + isAuthenticationSuccess +
                ", osVersion='" + osVersion + '\'' +
                '}';
    }
}
